﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TankLibrary
{
    public partial class Tank : UserControl
    {
        public Tank()
        {
            InitializeComponent();
            Level = 0;
            
        }


        public Tank(int level)
        {
            InitializeComponent();
            Level = level;
        }

        private int _level;
        public int Level {
            get { return _level; }
            set
            {
                if (_level > Size.Height)
                {
                    _level = 0;
                    throw new OverflowException("Tank is Overflow!");
                }
                _level = value;
            }
        }


        private void TankPaint(object sender, PaintEventArgs e)
        {
            e.Graphics.CompositingMode    = CompositingMode.SourceOver;
            e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
            FillTank(Level, e.Graphics);
        }
        
        Random _rand = new Random((int)DateTime.Now.Ticks);
        
        private void FillTank(int level, Graphics g)
        {
            if (level > Size.Height)
            {
                return;
                //throw new OverflowException("Tank is overflow!");
            }

            if (level <= 0)
            {

                return;
            }
            var graphics = new GraphicsPath();
            
            var volume   = new Rectangle(0, Size.Height - level, Size.Width, level);
            var points   = new List<Point>();
            
            graphics.AddRectangle(volume);
            
            points.Add(new Point(volume.X, volume.Y));
            int dx = volume.Width / 17; 
            for (int i = 0, n = volume.X; i < 17; i++, n+=dx)
            {
                points.Add(new Point(n, _rand.Next(volume.Y - 5, volume.Y + 5)));
            }
            
            points.Add(new Point(volume.X + volume.Width, volume.Y));
            graphics.AddBeziers(points.ToArray());
            //points.Clear();
            g.FillPath(new SolidBrush(Color.FromArgb(127, Color.Aqua.R, Color.Aqua.G, Color.Aqua.B)), graphics);
        }

        private void TankWaterWaveTick(object sender, EventArgs e)
        {
            Refresh();    
        }

    }
}
 